package com.qf.xiaomi.web.servlet;

import com.qf.xiaomi.pojo.PageBen;
import com.qf.xiaomi.pojo.TbGoodsKz;
import com.qf.xiaomi.service.impl.ProductServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 根据产品分类查询到对应产品
 * @Created by 马英超 on 2022/8/17 17:20
 * @author： 马英超
 * @version：v1.0
 * @description:
 */
public class ProductServlet {

    /**
     * 商品列表
     * @param req
     * @param resp
     * @return
     */

    private  String show(HttpServletRequest  req, HttpServletResponse resp){
        //获取参数
        final String id = req.getParameter("id");//类型ID
         String currentPage = req.getParameter("currentPage");//当前页
         String pageSize = req.getParameter("pageSize");//每页显示数

        //先判断ID是否为空，如果为空则返回error页面或首页
        if (id.isEmpty()){
            return "redirect:/index.jsp";
        }

        if (currentPage==null){
            //如果参数为空，则默认当前页为第一页
            currentPage="1";
        }
        if (pageSize==null){
            //如果参数为空，则默认每页显示条数为3
            pageSize="3";
        }
        //创建pageBen对象，并将对象的值传值service中，service返回一个pageBen对象，则当前页面可以调用pageBen内数据
        PageBen pageBen=new PageBen();
        try {
            pageBen.setCurrentPage(Integer.parseInt(currentPage));
            pageBen.setPageSize(Integer.parseInt(pageSize));
        }catch (Exception e){
            e.printStackTrace();
        }

        //创建service对象，并调用service方法
        ProductServiceImpl productService=new ProductServiceImpl();
        final PageBen pageBean = productService.queryProduct(id, pageBen);

        if (pageBean!=null){
            System.out.println(pageBean);
            req.setAttribute("pageBean",pageBean);
            System.out.println("pageBen已存入请求域");
            //请求转发
            return "forward:/goodsList.jsp";
        }
        return "redirect:/error/error.jsp";
    }

    /**
     * 商品详情
     * @param req
     * @param resp
     * @return
     */
    private String detail(HttpServletRequest req,HttpServletResponse resp){
        //获取请求参数
        final String produceId = req.getParameter("id");//根据ID获取商品信息
        //查看上平详细信息需要：product键、id、picture、name、price、pubdate、intro
        //以上信息通过Service调用Dao查询返回商品信息
        ProductServiceImpl productService=new ProductServiceImpl();
        final TbGoodsKz product = productService.queryGoodsById(produceId);
        if(product!=null){
            //将对象存入请求域内
            req.setAttribute("product",product);

            return "forward:/goodsDetail.jsp";
        }
        return "redirect:/goodsList.jsp";
    }

    private String add(HttpServletRequest req,HttpServletResponse resp){


        //获取参数
        final String name = req.getParameter("name");
        final String typeid = req.getParameter("typeid");
        final String pubdate = req.getParameter("pubdate");
        final String price = req.getParameter("price");
        final String star = req.getParameter("star");
        final String picture = req.getParameter("picture");
        final String intro = req.getParameter("intro");
        System.out.println(picture);

    return "redirect:/goods.jsp";
    }


}